//
//  SelectGoldView.swift
//  Box
//
//  Created by mn on 2025/4/3.
//

import UIKit
import SnapKit

struct SelectGoldItem {
    let gold: Int
    let price: Int
}

class SelectGoldView: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var selectIndex = 0 {
        didSet{
            collectionView.reloadData()
        }
    }
    var selectedAction: (() -> Void)?

    private let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10  // item 之间的最小间距
        layout.minimumLineSpacing = 10 // 行间距
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        return collectionView
    }()
    
    var items = [SelectGoldItem]()
    var amount: Int?{
        if selectIndex < 0 {
            return nil
        }
        return items[selectIndex].gold
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCollectionView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupCollectionView()
    }
    
    private func setupCollectionView() {
        
        items = [10, 40, 98, 198, 488, 998, 1888, 4888].map { SelectGoldItem(gold: $0, price: $0 * 100) }
        
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 13, left: 18, bottom: 13, right: 18))
        }
        
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.registerCell(SelectGoldCell.self)
    }
    
    // MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:SelectGoldCell = collectionView.cell(indexPath)
        cell.priceLabel.text = String(format: "¥%.2f", Float(items[indexPath.item].price) * 0.01)
        cell.goldLabel.text = "\(items[indexPath.item].gold)"
        cell.contentView.layer.borderColor = indexPath.item == selectIndex ? UIColor.init(hex: "#1B3B49").cgColor : UIColor.clear.cgColor
        return cell
    }
    
    // MARK: - UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let totalSpacing: CGFloat = 18 * 2 + 10 * 2 // 左右边距 + 两个间隔
        let itemWidth = (UIScreen.main.bounds.width - totalSpacing) / 3 - 1
        return CGSize(width: itemWidth, height: 80.5)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedAction?()
        selectIndex = indexPath.item
        collectionView.reloadData()
    }
}
